//
//  AListCell.m
//  MClient
//
//  Created by zhouyi on 10-12-20.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "AListCell.h"


@implementation AListCell


- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
       self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

-(void)addFieldOfCell:aListInfo {
	float startX = 0.0;
	float spaceX = 10.0;
	float cellHeight = [[[aListInfo objectAtIndex:([aListInfo count] - 1)] objectForKey:@"cellHeight"] floatValue];
	for (NSMutableDictionary *cellInfo in aListInfo) {
		NSString *fieldValue = [cellInfo objectForKey:@"value"];
		NSString *fieldType = [cellInfo objectForKey:@"fieldType"];
		if ([fieldType isEqualToString:@"S"]) {
			continue;
		}
		float fieldWidth = [[cellInfo objectForKey:@"fieldWidth"] floatValue];
		float fieldHeight = [[cellInfo objectForKey:@"fieldHeight"] floatValue];
		int fieldOrder = [[cellInfo objectForKey:@"fieldOrder"] intValue];
		float fontSize = [[cellInfo objectForKey:@"fontSize"] floatValue];
		int row = [[cellInfo objectForKey:@"row"] intValue];
		float startY = [[cellInfo objectForKey:@"startY"] floatValue];
		NSString *fontColor = [cellInfo objectForKey:@"fontColor"];
		NSString *fontStyle = [cellInfo objectForKey:@"fontStyle"];
		NSString *fontAlign = [cellInfo objectForKey:@"fontAlign"];
		if (fieldOrder == 1) {
			startX = 0;
		}
		UILabel *title = [[UILabel alloc] init];
		title.backgroundColor = [UIColor clearColor];
		[title setTextAlignment:UITextAlignmentLeft];
		if ([fieldType isEqualToString:@"H"] 
			|| [fieldType isEqualToString:@"K"]
			|| [fieldType isEqualToString:@"I"] 
			|| [fieldType isEqualToString:@"L"]){
			NSArray *selectIcons = [fieldValue componentsSeparatedByString:@"::"];
			if ([selectIcons count] > 1) {
				NSString *iconLabel = [selectIcons objectAtIndex:1];
				NSArray *icons = [iconLabel componentsSeparatedByString:@","];
				CGRect rect = CGRectMake(spaceX + startX, startY, fieldWidth, fieldHeight);
				[title setFrame:rect];
				for (int j = 0; j < [icons count]; j++) {
					NSString *icon = [icons objectAtIndex:j];
					UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(28 * j, 2, 24, 24)];
					imageView.image = [MCFunction scaleAndRotateImage:[UIImage imageNamed:icon] width:24.0 height:24.0];
					imageView.backgroundColor = [UIColor clearColor];
					[title addSubview:imageView];
					[imageView release];
				}
				[title setNumberOfLines:row];
				[self addSubview:title];
				[title release];
				startX = startX + fieldWidth;
				continue;
			} 
		}
		
		if ([fontStyle isEqualToString:@"B"]) {
			[title setFont:[UIFont boldSystemFontOfSize:fontSize]];
		}else if ([fontStyle isEqualToString:@"I"]) {
			[title setFont:[UIFont italicSystemFontOfSize:fontSize]];
		}else {
			[title setFont:[UIFont systemFontOfSize:fontSize]];
		}
		UIColor *color = [MCFunction hexColor:[NSString stringWithFormat:@"%@64", fontColor]];
		[title setTextColor:color];
		
		CGSize size = [fieldValue sizeWithFont:title.font];
		CGRect tRect = CGRectMake(spaceX + startX, startY, fieldWidth, size.height*row);
		if (cellHeight < 28.0) {
			tRect = CGRectMake(spaceX + startX, (44.0 - size.height*row)/2 - 3.0, fieldWidth, size.height*row);
		}
		[title setFrame:tRect];
		[title setNumberOfLines:row];
		[title setLineBreakMode:UILineBreakModeTailTruncation];
		[title setText:fieldValue];
		if ([fieldType isEqualToString:@"6"]
			||[fieldType isEqualToString:@"7"]
			||[fieldType isEqualToString:@"E"]
			||[fieldType isEqualToString:@"1"]){
			title.textAlignment = UITextAlignmentRight;
		}else {
			title.textAlignment = UITextAlignmentLeft;
		}
		if ([fontAlign length]>0) {
			if ([fontAlign isEqualToString:@"L"]) {
				title.textAlignment = UITextAlignmentLeft;
			}else if ([fontAlign isEqualToString:@"R"]) {
				title.textAlignment = UITextAlignmentRight;
			}else if ([fontAlign isEqualToString:@"M"]) {
				title.textAlignment = UITextAlignmentCenter;
			}
		}
		[self addSubview:title];
		[title release];
		startX = startX + fieldWidth;
	}
}

- (void)dealloc {
    [super dealloc];
}


@end
